package boundary;

import java.awt.GridBagLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Font;
import javax.swing.JButton;
import dataview.Menus;
import exceptions.DBException;
import store.Menu;
import store.TuttiMenu;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.JScrollPane;
import javax.swing.DefaultListModel;
import javax.swing.JRadioButton;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import javax.swing.ImageIcon;
import java.awt.Cursor;

public class ListaMenu extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private Home main;
	private Menus menus = null;  //  @jve:decl-index=0:
	private JLabel mainLabel = null;
	private JButton eliminaButton = null;
	private JButton modificaButton = null;
	private JButton reportB = null;
	private JScrollPane jScrollPane = null;
	private JRadioButton ridottaRB = null;
	private JRadioButton completaRB = null;
	private ButtonGroup bg = new ButtonGroup();
	private JList jList = null;
	
	private JLabel getMainLabel () {
		if (mainLabel == null) {
			mainLabel = new JLabel();
			mainLabel.setFont(new Font("Dialog", Font.BOLD, 24));
		}
		return mainLabel;
	}
	
	public void setTitle (String text) {
		getMainLabel().setText(text);
	}
	
	public void setMenu(Menus m) {
		menus = m;
		DefaultListModel model = new DefaultListModel();
		if (menus == null) {
			try {
				menus = TuttiMenu.getTuttiMenu().tuttiMenu();
				getMainLabel().setText("Archivio Menu");
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
		else getMainLabel().setText("Risultati Ricerca");

		for (Menu x : menus)
			model.addElement(x);
		getJList().setModel(model);
		getReportB().setEnabled(menus != null && !menus.isEmpty());
		getModificaButton().setEnabled(false);
		getEliminaButton().setEnabled(false);
	}
	
	/**
	 * This is the default constructor
	 */
	public ListaMenu(Home home, Menus risultatoRic) {
		super();
		this.main = home;
		setMenu(risultatoRic);
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gbcCompletaRB = new GridBagConstraints();
		gbcCompletaRB.gridx = 1;
		gbcCompletaRB.anchor = GridBagConstraints.WEST;
		gbcCompletaRB.insets = new Insets(3, 5, 5, 5);
		gbcCompletaRB.gridy = 2;
		GridBagConstraints gbcRidottaRB = new GridBagConstraints();
		gbcRidottaRB.gridx = 0;
		gbcRidottaRB.insets = new Insets(3, 5, 5, 5);
		gbcRidottaRB.anchor = GridBagConstraints.WEST;
		gbcRidottaRB.gridy = 2;
		GridBagConstraints gbcJScrollPane = new GridBagConstraints();
		gbcJScrollPane.fill = GridBagConstraints.BOTH;
		gbcJScrollPane.weighty = 1.0;
		gbcJScrollPane.gridx = 0;
		gbcJScrollPane.gridy = 1;
		gbcJScrollPane.gridwidth = 4;
		gbcJScrollPane.weightx = 1.0;
		gbcJScrollPane.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gbcReportB = new GridBagConstraints();
		gbcReportB.gridx = 3;
		gbcReportB.insets = new Insets(5, 5, 5, 5);
		gbcReportB.anchor = GridBagConstraints.WEST;
		gbcReportB.gridy = 3;
		GridBagConstraints gbcModificaB = new GridBagConstraints();
		gbcModificaB.gridx = 0;
		gbcModificaB.insets = new Insets(5, 5, 5, 5);
		gbcModificaB.anchor = GridBagConstraints.WEST;
		gbcModificaB.gridy = 3;
		GridBagConstraints gbcEliminaB = new GridBagConstraints();
		gbcEliminaB.gridx = 1;
		gbcEliminaB.anchor = GridBagConstraints.WEST;
		gbcEliminaB.insets = new Insets(5, 5, 5, 5);
		gbcEliminaB.gridy = 3;
		GridBagConstraints gbcMainL = new GridBagConstraints();
		gbcMainL.gridx = 0;
		gbcMainL.gridwidth = 4;
		gbcMainL.insets = new Insets(5, 3, 5, 3);
		gbcMainL.gridy = 0;
		getMainLabel().setText("Risultati Ultima Ricerca");
		this.setSize(476, 265);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		this.add(getMainLabel(), gbcMainL);
		this.add(getEliminaButton(), gbcEliminaB);
		this.add(getModificaButton(), gbcModificaB);
		this.add(getReportB(), gbcReportB);
		this.add(getJScrollPane(), gbcJScrollPane);
		this.add(getRidottaRB(), gbcRidottaRB);
		this.add(getCompletaRB(), gbcCompletaRB);
		
		bg.add(getRidottaRB());
		bg.add(getCompletaRB());
	}
	
	/**
	 * This method initializes eliminaButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEliminaButton() {
		if (eliminaButton == null) {
			eliminaButton = new JButton();
			eliminaButton.setText("Elimina");
			eliminaButton.setIcon(new ImageIcon(getClass().getResource("/icone/blog-post-delete-icon.png")));
			eliminaButton.setFont(new Font("Dialog", Font.BOLD, 12));
			eliminaButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			eliminaButton.setEnabled(false);
			eliminaButton.addActionListener(this);
		}
		return eliminaButton;
	}
	
	/**
	 * This method initializes modificaButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getModificaButton() {
		if (modificaButton == null) {
			modificaButton = new JButton();
			modificaButton.setText("Modifica");
			modificaButton.setIcon(new ImageIcon(getClass().getResource("/icone/blog-post-edit-icon.png")));
			modificaButton.setFont(new Font("Dialog", Font.BOLD, 12));
			modificaButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			modificaButton.setEnabled(false);
			modificaButton.addActionListener(this);
		}
		return modificaButton;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == reportB) {
			new SelezionaTipoReport(main, SelezionaTipoReport.TipoDato.MENU, menus).setVisible(true);
			return;
		}
		if (e.getSource() == modificaButton) {
			main.modificareMenu((Menu)getJList().getSelectedValue());
		} else if (e.getSource() == eliminaButton) {
			// non sarebbe meglio chiamare eliminacibo passando il nome e non il
			// cibo come parametro?
			main.eliminareMenu((Menu)getJList().getSelectedValue());
			main.mostraListaMenu(null);
		}
	}
	
	/**
	 * This method initializes reportB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getReportB() {
		if (reportB == null) {
			reportB = new JButton();
			reportB.setText("Report");
			reportB.setIcon(new ImageIcon(getClass().getResource("/icone/attachment-icon.png")));
			reportB.setFont(new Font("Dialog", Font.BOLD, 12));
			reportB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			reportB.setEnabled(false);
			reportB.addActionListener(this);
		}
		return reportB;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}
	
	public void aggiornaVista() {
		getModificaButton().setEnabled(false);
		getEliminaButton().setEnabled(false);
		
		getJList().setCellRenderer(new MyListCellRenderer(getCompletaRB().isSelected()));
		getJList().repaint();
	}
	
	/**
	 * This method initializes ridottaRB
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRidottaRB() {
		if (ridottaRB == null) {
			ridottaRB = new JRadioButton();
			ridottaRB.setText("Vista ridotta");
			ridottaRB.setFont(new Font("Dialog", Font.BOLD, 12));
			ridottaRB.setSelected(true);
			ridottaRB.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					aggiornaVista();
					getModificaButton().setEnabled(getJList().getSelectedIndex() != -1) ;
					getEliminaButton().setEnabled(getJList().getSelectedIndex() != -1) ;
				}
			});
		}
		return ridottaRB;
	}
	
	/**
	 * This method initializes completaRB
	 * 
	 * @return javax.swing.JRadioButton
	 */
	JRadioButton getCompletaRB() {
		if (completaRB == null) {
			completaRB = new JRadioButton();
			completaRB.setText("Vista completa");
			completaRB.setFont(new Font("Dialog", Font.BOLD, 12));
			completaRB.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					aggiornaVista();
					getModificaButton().setEnabled(getJList().getSelectedIndex() != -1) ;
					getEliminaButton().setEnabled(getJList().getSelectedIndex() != -1) ;
				}
			});
		}
		return completaRB;
	}
	
	/**
	 * This method initializes jList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList();
			jList.setCellRenderer(new MyListCellRenderer(false));
			jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jList.setAutoscrolls(true);
			jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
				public void valueChanged(javax.swing.event.ListSelectionEvent e) {
					getModificaButton().setEnabled(getJList().getSelectedIndex() != -1);
					getEliminaButton().setEnabled(getJList().getSelectedIndex() != -1);
				}
			});
			jList.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (getJList().getSelectedIndex() != -1) {
						switch (e.getKeyCode()) {
							case KeyEvent.VK_ENTER:
								main.modificareMenu((Menu)getJList().getSelectedValue());
								break;
							case KeyEvent.VK_DELETE:
							case KeyEvent.VK_BACK_SPACE:
								main.eliminareMenu((Menu)getJList().getSelectedValue());
								main.mostraListaMenu(null);
								break;
							default:
								break;
						}
					}
				}
			});
		}
		return jList;
	}
} //  @jve:decl-index=0:visual-constraint="10,10"

